using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;
using System.Collections;
using System.IO;

namespace AdaptiveHelpDesk
{
    class GetTaskModel
    {
        XmlElement Steps;
        XmlElement SearchCustomer;
        XmlElement currentUser; 

        XmlElement LogCall;
        XmlElement CallCat;
        XmlElement CallClass;

        XmlElement AssignCall;

        XmlElement ProvideSol;
        XmlElement SolClass;

        XmlElement Current;

        //Working Data Variables

        XmlElement CallID;
        XmlElement LogginDate;
        XmlElement LogginTime;
        XmlElement CurrentCustomer;
        XmlElement Stereotype;

        //Working Data file xml loaded to working data file
        String wd = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, @"Knowledge Base\\Task Model\\WorkingData.xml");
        XmlDocument wd_doc = new XmlDocument();

        //Task Model file.
        String tm = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, @"Knowledge Base/Task Model/TaskModel.xml");
        XmlDocument doc = new XmlDocument();

        public GetTaskModel()
        {   
            doc.Load(tm);

            currentUser = (XmlElement)doc.DocumentElement.FirstChild;
            Steps = (XmlElement)doc.DocumentElement.FirstChild.NextSibling.NextSibling; //Third sibling
            Current = (XmlElement)doc.DocumentElement.LastChild;

            //Load Working Data XML file
            wd_doc.Load(wd);

            CallID = (XmlElement)wd_doc.DocumentElement.FirstChild;
            LogginDate = (XmlElement)CallID.NextSibling;
            LogginTime = (XmlElement)LogginDate.NextSibling;
            CurrentCustomer = (XmlElement)LogginTime.NextSibling;
            Stereotype = (XmlElement)CurrentCustomer.NextSibling;

            ////Search Customer
            Step1SearchCustomer();

            ////Log A Call
            Step2LogACall();
            //getCallCateg();
            //getCallClass();

            ////Assign Call
            Step3AssignCall();

            ////Provide Solution
            Step4ProvideSolution();
            //SolClassification();
        }

        //1 SEARCH CUSTOMER
        #region 
            private void Step1SearchCustomer()
            {
                SearchCustomer = (XmlElement)Steps.GetElementsByTagName("SearchCustomer")[0];
            }

                public string getSearchBy()
                {
                    return SearchCustomer.ChildNodes[0].InnerText;
                }

                public string getSearchValue()
                {
                    return SearchCustomer.ChildNodes[1].InnerText;
                }

                public string getSelectCustomer()
                {
                    return SearchCustomer.ChildNodes[2].InnerText;
                }

                public ArrayList getCustomerDetailsSteps()
                {
                    return getStepNames(SearchCustomer);
                }

                public string getSearchCustomerName()
                {
                    return getStepName(SearchCustomer);
                }

                public string getSearchCustomerWSDL()
                {
                    return getWSDL(SearchCustomer);
                }

                public string getSearchCustomerStatus()
                {
                    XmlElement Complete = (XmlElement)SearchCustomer.LastChild;
                    return Complete.InnerText;
                }
                public void setSearchCustomerStatus(string status)
                {
                    XmlElement Complete = (XmlElement)SearchCustomer.LastChild;
                    Complete.InnerText = status;
                    UpdateTaskModel();
                }

                
        #endregion

        //2 LOG CALL
        #region
            private void Step2LogACall()
            {
                LogCall = (XmlElement)Steps.GetElementsByTagName("LogCall")[0];
            }

                public string getCallDescr()
                {
                    return LogCall.ChildNodes[0].InnerText;
                }

                private void getCallCateg()
                {
                    CallCat = (XmlElement)LogCall.GetElementsByTagName("Call_Categorisation")[0];
                }

                    public string getServiceName()
                    {
                        return CallCat.ChildNodes[0].InnerText;
                    }

                    public string getCallType()
                    {
                        return CallCat.ChildNodes[1].InnerText;
                    }

                    public string getSubCallType()
                    {
                        return CallCat.ChildNodes[2].InnerText;
                    }

                private void getCallClass()
                {
                    CallClass = (XmlElement)LogCall.GetElementsByTagName("Call_Classification")[0];
                }

                    public string getPriority()
                    {
                        return CallClass.ChildNodes[0].InnerText;
                    }

                    public string getSource()
                    {
                        return CallClass.ChildNodes[1].InnerText;
                    }


                public ArrayList getLogCallSteps()
                {
                    return getStepNames(LogCall);
                }

                public string getLogACallStatus()
                {
                    XmlElement Complete = (XmlElement)LogCall.LastChild;
                    return Complete.InnerText;
                }
                public void setLogACallStatus(string status)
                {
                    XmlElement Complete = (XmlElement)LogCall.LastChild;
                    Complete.InnerText = status;
                    UpdateTaskModel();
                }
        #endregion


        //3 ASSIGN A CALL
        #region
            private void Step3AssignCall()
            {
               AssignCall = (XmlElement)Steps.GetElementsByTagName("AssignCall")[0];
            }

                public string getCampus()
                {
                    return AssignCall.ChildNodes[0].InnerText;
                }

                public string getContact()
                {
                    return AssignCall.ChildNodes[1].InnerText;
                }

                public ArrayList getAssignCallSteps()
                {
                    return getStepNames(AssignCall);
                }

                public string getAssignCallStatus()
                {
                    XmlElement Complete = (XmlElement)AssignCall.LastChild;
                    return Complete.InnerText;
                }
                public void setAssignCallStatus(string status)
                {
                    XmlElement Complete = (XmlElement)AssignCall.LastChild;
                    Complete.InnerText = status;
                    UpdateTaskModel();
                }

        #endregion


        //4 PROVIDE SOLUTION
        #region
            private void Step4ProvideSolution()
            {
                ProvideSol = (XmlElement)Steps.GetElementsByTagName("ProvideSolution")[0];
            }

                public string getSolDescr()
                {
                    return ProvideSol.ChildNodes[0].InnerText;
                }

                private void SolClassification()
                {
                    SolClass = (XmlElement)ProvideSol.GetElementsByTagName("Solution_Classification")[0];
                }

                    public string getCause()
                    {
                        return SolClass.ChildNodes[0].InnerText;
                    }

                    public string getHowResolved()
                    {
                        return SolClass.ChildNodes[1].InnerText;
                    }

                public ArrayList getProvideSolutionSteps()
                {
                    return getStepNames(ProvideSol);
                }

                public string getProvideSolutionStatus()
                {
                    XmlElement Complete = (XmlElement)ProvideSol.LastChild;
                    return Complete.InnerText;
                }
                public void setProvideSolutionStatus(string status)
                {
                    XmlElement Complete = (XmlElement)ProvideSol.LastChild;
                    Complete.InnerText = status;
                    UpdateTaskModel();
                }

         #endregion


         //Arraylist containing step names
         private ArrayList getStepNames(XmlElement e)
         {
             ArrayList steps = new ArrayList();

             for (int i = 0; i < e.ChildNodes.Count; i++)
             {
                 steps.Add(e.ChildNodes[i].Name);
             }
             return steps;
         }



         #region Emile:- XML helper methods
         public String GetCallID()
         {
             return CallID.InnerText;
         }
         public String GetLogginDate()
         {
             return LogginDate.InnerText;
         }
         public String GetLogginTime()
         {
             return LogginTime.InnerText;
         }
         public String GetSelectedCustomer()
         {
             return CurrentCustomer.InnerText;
         }
         public String GetStereotype()
         {
             return Stereotype.InnerText;
         }

         public void SetSelectedCustomer(String value)
         {
             CurrentCustomer.InnerText = value;
             UpdateXML();
         }
         public void SetCallID(String value)
         {
             CallID.InnerText = value;
             UpdateXML();
         }
         public void SetLogginDate(String value)
         {
             LogginDate.InnerText = value;
             UpdateXML();
         }
         public void SetLogginTime(String value)
         {
             LogginTime.InnerText = value;
             UpdateXML();
         }
         public void SetStereoptype(String value)
         {
             Stereotype.InnerText = value;
             UpdateXML();
         }         
         public String getElementValue(String elementName){
             XmlNodeList elements = (XmlNodeList)wd_doc.GetElementsByTagName(elementName);
             String value = elements[0].InnerText;
             return value;
         }
         private void UpdateXML()
         {
             wd_doc.Save(wd);
         }

         private void UpdateTaskModel()
         {
             doc.Save(tm);
         }
         private String getStepName(XmlElement x)
         {
             return x.GetAttribute("name");
         }

         private String getWSDL(XmlElement x)
         {
             return x.GetAttribute("wsdl");
         }

         private String getOperation(XmlElement x)
         {
             return x.GetAttribute("operation");
         }

         public String CheckCurrentUser() //move to user model
         {
             try
             {
                 return currentUser.InnerText;
             }
             catch (Exception ex)
             {                 
                 return ex.StackTrace;
             }            
         }
         #endregion
    }
}
